#include "stdafx.h"

#include "AllLookFeel.h"
#include "WidgetCreateDlg.h"

WidgetCreateDlg::WidgetCreateDlg()
	: mWidget(NULL)
{
	mLayout = new MGUI_Layout(AllLookFeel::Instance()->GetWhite());
	mLayout->SetAlign(MGUI_Align::Stretch);
	mLayout->SetOrder(MGUI_Order::Top);
	mLayout->SetColor(MGUI_Color(0.15f, 0.15f, 0.15f, 0.5f));

	// Dialog
	mDialog = new MGUI_Widget(AllLookFeel::Instance()->GetDialog(), mLayout);
	mDialog->SetAlign(MGUI_Align::Center);
	mDialog->SetRect(MGUI_Rect(true, 0, 0, 712, 573));

	mLabel_Caption = new MGUI_Label(AllLookFeel::Instance()->GetDialogCaption(), mDialog);
	mLabel_Caption->SetCaption(L"WidgetCreate");
	mLabel_Caption->SetAlign(MGUI_Align::Top | MGUI_Align::HStretch);

	// Left
	int x_base = 0;
	int y_base = mLabel_Caption->GetRect().y1 + 5;

	mComboBox_WidgetType = new MGUI_ComboBox(AllLookFeel::Instance()->GetComboBox(), mDialog);
	mComboBox_WidgetType->SetRectEx(0, y_base, 200, 24);
	y_base += 24 + 2;

	mListBox_LookFeel = new MGUI_ListBox(AllLookFeel::Instance()->GetListBox(), mDialog);
	mListBox_LookFeel->SetRectEx(0, y_base, 200, 500);
	y_base += 256;
	x_base += 200 + 5;

	// Right
	y_base = mLabel_Caption->GetRect().y1 + 5;

	mWidget_Priview = new MGUI_Widget(AllLookFeel::Instance()->GetPanel(), mDialog);
	mWidget_Priview->SetRect(MGUI_Rect(true, x_base, y_base, 500, 500));
	y_base += 500 + 2;
	x_base += 235;

	mButton_OK = new MGUI_Button(AllLookFeel::Instance()->GetButton(), mDialog);
	mButton_OK->SetRectEx(x_base, y_base, 128, 24);
	mButton_OK->SetCaption(L"OK");
	x_base += 128 + 5;

	mButton_Cancel = new MGUI_Button(AllLookFeel::Instance()->GetButton(), mDialog);
	mButton_Cancel->SetRectEx(x_base, y_base, 128, 24);
	mButton_Cancel->SetCaption(L"Cancel");
	y_base += 24 + 2;

	mLayout->SetVisible(false);


	// Init
	for (int i = 0; i < MGUI_WidgetType::Max; ++i)
		mComboBox_WidgetType->Append(MGUI_WidgetType::TypeString[i].c_wstr());

	mComboBox_WidgetType->eventSelectChanged += OnWidgetTypeChanged(this, &WidgetCreateDlg::OnWidgetTypeChanged_);
	mListBox_LookFeel->eventSelectChanged += OnLookFeelChanged(this, &WidgetCreateDlg::OnLookFeelChanged_);
	mButton_OK->eventMouseClick += OnOK(this, &WidgetCreateDlg::OnOK_);
	mButton_Cancel->eventMouseClick += OnCancel(this, &WidgetCreateDlg::OnCancel_);

	mComboBox_WidgetType->SetSelectIndex(0);
}

WidgetCreateDlg::~WidgetCreateDlg()
{
	delete mLayout;
}

void WidgetCreateDlg::DoModal()
{
	OnWidgetTypeChanged_(mComboBox_WidgetType->GetSelectIndex());
	mLayout->SetVisible(true);
}

int WidgetCreateDlg::GetSelectType()
{
	return mComboBox_WidgetType->GetSelectIndex();
}

MGUI_LookFeel * WidgetCreateDlg::GetSelectLookFeel()
{
	if (mListBox_LookFeel->GetSelectItem())
	{
		return (MGUI_LookFeel *)mListBox_LookFeel->GetSelectItem()->GetUserData();
	}

	return NULL;
}

void WidgetCreateDlg::OnWidgetTypeChanged_(int _index)
{
	OnLookFeelChanged_(-1);

	mListBox_LookFeel->Clear();

	if (_index == -1)
		return ;

	int type = _index;

	for (int i = 0; i < MGUI_LookFeelManager::Instance()->GetLookFeelCount(type); ++i)
	{
		MGUI_LookFeel * lk = MGUI_LookFeelManager::Instance()->GetLookFeel(type, i);

		mListBox_LookFeel->Append(lk->GetName().c_wstr(), lk);
	}
}

void WidgetCreateDlg::OnLookFeelChanged_(int _index)
{
	safe_delete(mWidget);

	if (_index == -1)
		return ;

	int type = mComboBox_WidgetType->GetSelectIndex();

	if (type == -1)
		return ;

	MGUI_LookFeel * lk = (MGUI_LookFeel *)mListBox_LookFeel->GetUserData(_index);

	mWidget = MGUI_Helper::Instance()->CreateWidget(type, lk, mWidget_Priview);
	mWidget->SetAlign(MGUI_Align::Center);
}

void WidgetCreateDlg::OnOK_()
{
	eventEndDialog(true);
	mLayout->SetVisible(false);
}

void WidgetCreateDlg::OnCancel_()
{
	eventEndDialog(false);
	mLayout->SetVisible(false);
}